<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>Module: ActiveRecord::Locking::Optimistic</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <meta http-equiv="Content-Script-Type" content="text/javascript" />
  <link rel="stylesheet" href="../../.././rdoc-style.css" type="text/css" media="screen" />
  <script type="text/javascript">
  // <![CDATA[

  function popupCode( url ) {
    window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
  }

  function toggleCode( id ) {
    if ( document.getElementById )
      elem = document.getElementById( id );
    else if ( document.all )
      elem = eval( "document.all." + id );
    else
      return false;

    elemStyle = elem.style;
    
    if ( elemStyle.display != "block" ) {
      elemStyle.display = "block"
    } else {
      elemStyle.display = "none"
    }

    return true;
  }
  
  // Make codeblocks hidden by default
  document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
  
  // ]]>
  </script>

</head>
<body>



    <div id="classHeader">
        <table class="header-table">
        <tr class="top-aligned-row">
          <td><strong>Module</strong></td>
          <td class="class-name-in-header">ActiveRecord::Locking::Optimistic</td>
        </tr>
        <tr class="top-aligned-row">
            <td><strong>In:</strong></td>
            <td>
                <a href="../../../files/vendor/rails/activerecord/lib/active_record/locking/optimistic_rb.html">
                vendor/rails/activerecord/lib/active_record/locking/optimistic.rb
                </a>
        <br />
            </td>
        </tr>

        </table>
    </div>
  <!-- banner header -->

  <div id="bodyContent">



  <div id="contextContent">

    <div id="description">
      <h2>What is <a href="Optimistic.html">Optimistic</a> <a href="../Locking.html">Locking</a></h2>
<p>
<a href="Optimistic.html">Optimistic</a> locking allows multiple users to
access the same record for edits, and assumes a minimum of conflicts with
the data. It does this by checking whether another process has made changes
to a record since it was opened, an <a
href="../StaleObjectError.html">ActiveRecord::StaleObjectError</a> is
thrown if that has occurred and the update is ignored.
</p>
<p>
Check out <a href="Pessimistic.html">ActiveRecord::Locking::Pessimistic</a>
for an alternative.
</p>
<h2>Usage</h2>
<p>
Active Records support optimistic locking if the field
<tt>lock_version</tt> is present. Each update to the record increments the
lock_version column and the locking facilities ensure that records
instantiated twice will let the last one saved raise a <a
href="../StaleObjectError.html">StaleObjectError</a> if the first was also
updated. Example:
</p>
<pre>
  p1 = Person.find(1)
  p2 = Person.find(1)

  p1.first_name = &quot;Michael&quot;
  p1.save

  p2.first_name = &quot;should fail&quot;
  p2.save # Raises a ActiveRecord::StaleObjectError
</pre>
<p>
You&#8216;re then responsible for dealing with the conflict by rescuing the
exception and either rolling back, merging, or otherwise apply the business
logic needed to resolve the conflict.
</p>
<p>
You must ensure that your database schema defaults the lock_version column
to 0.
</p>
<p>
This behavior can be turned off by setting
<tt>ActiveRecord::Base.lock_optimistically = false</tt>. To override the
name of the lock_version column, invoke the <tt>set_locking_column</tt>
method. This method uses the same syntax as <tt>set_table_name</tt>
</p>

    </div>


   </div>


  </div>


    <!-- if includes -->

    <div id="section">

    <div id="class-list">
      <h3 class="section-bar">Classes and Modules</h3>

      Module <a href="Optimistic/ClassMethods.html" class="link">ActiveRecord::Locking::Optimistic::ClassMethods</a><br />

    </div>




      


    <!-- if method_list -->


  </div>


<div id="validator-badges">
  <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
</div>

</body>
</html>